package com.example.administrator.wallet.view.fragment;


import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.CoinChangeAdapter;
import com.example.administrator.wallet.adapter.TransactionAdapter;
import com.example.administrator.wallet.base.BaseFragment;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.MainBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class TransactionFragment extends BaseFragment {


    @Bind(R.id.coin_change)
    TextView coinChange;
    @Bind(R.id.release)
    TextView release;
    @Bind(R.id.buy)
    TextView buy;
    @Bind(R.id.sell)
    TextView sell;
    @Bind(R.id.coin_balance)
    TextView coinBalance;
    @Bind(R.id.conver)
    TextView conver;
    @Bind(R.id.mList)
    ListView mList;
    @Bind(R.id.loadError)
    TextView loadError;
    @Bind(R.id.mParentView)
    LinearLayout mParentView;
    @Bind(R.id.release_true)
    LinearLayout releaseTrue;
    @Bind(R.id.price)
    TextView price;
    @Bind(R.id.price_linear)
    LinearLayout priceLinear;
    @Bind(R.id.rate)
    TextView rate;
    @Bind(R.id.rate_linear)
    LinearLayout rateLinear;
    @Bind(R.id.zifu)
    TextView zifu;
    @Bind(R.id.counts)
    EditText counts;
    @Bind(R.id.price_money)
    TextView priceMoney;
    @Bind(R.id.trans_linear)
    LinearLayout transLinear;
    @Bind(R.id.select_mode)
    TextView selectMode;
    @Bind(R.id.zhifubao_select)
    ImageView zhifubaoSelect;
    @Bind(R.id.zhifubao)
    LinearLayout zhifubao;
    @Bind(R.id.weixin_select)
    ImageView weixinSelect;
    @Bind(R.id.weixin)
    LinearLayout weixin;
    @Bind(R.id.yinhangka_select)
    ImageView yinhangkaSelect;
    @Bind(R.id.yinhangka)
    LinearLayout yinhangka;
    @Bind(R.id.lists)
    LinearLayout lists;
    @Bind(R.id.total_price)
    TextView totalPrice;
    @Bind(R.id.total)
    LinearLayout total;
    @Bind(R.id.buy_submit)
    TextView buySubmit;
    @Bind(R.id.release_select)
    RelativeLayout releaseSelect;
    @Bind(R.id.is_release)
    RelativeLayout isRelease;
    @Bind(R.id.coin_icons)
    ImageView coinIcons;
    @Bind(R.id.coin_counts)
    TextView coinCounts;
    @Bind(R.id.sold_out)
    TextView soldOut;
    @Bind(R.id.sell_statu)
    LinearLayout sellStatu;
    private View popup_view;
    private ListView mListView;
    private PopupWindow mWindow;
    private IRequestManager requestManager;
    private String token;
    private List<MainBean.DataBean.ListBean> changeData;
    private Gson mGson;
    public static CoinChangeAdapter changeAdapter;
    private int pst;
    public static int position;
    private LinearLayout mLinears;
    private TransactionAdapter transAdapter;
    private int release_statu = 0;
    public static final int ZHIFUBAO = 0;
    public static final int WEIXIN = 1;
    public static final int YINHANGKA = 2;

    @Override
    public void init() {
        initManager();
        initShareData();
        initView();
        coinChangeRequestData();
        initData();
        initAdapter();
        load();
    }

    private void load() {
        if (transAdapter.getCount() > 0) {
            loadError.setVisibility(View.GONE);
        }
    }

    private void initAdapter() {
        transAdapter = new TransactionAdapter(getActivity());
        transAdapter.statu = position;
        mList.setAdapter(transAdapter);
    }

    private void initData() {

    }

    private void initShareData() {
        token = SharedPrefsUtil.getValue(getActivity(), "wallet", "token", null);
        position = SharedPrefsUtil.getValue(getActivity(), "wallet", "select", 0);
    }

    private void initManager() {
        requestManager = RequestFactory.getReqequestManager();
        mGson = new Gson();
    }

    private void initView() {
        release.setSelected(true);
        viewSelected();
        if (position == 0) {
            buy();
        } else {
            sell();
        }
    }

    @Override
    public int getLayoutResource() {
        return R.layout.fragment_transaction;
    }

    @OnClick({R.id.sold_out, R.id.coin_change, R.id.release, R.id.buy, R.id.sell, R.id.conver, R.id.zhifubao, R.id.weixin, R.id.yinhangka, R.id.buy_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.coin_change:
                coinChange();
                break;
            case R.id.release:
                release();
                break;
            case R.id.buy:
                hideMode();
                viewSelected();
                buy();
                break;
            case R.id.sell:
                hideMode();
                viewSelected();
                sell();
                break;
            case R.id.conver:
                break;
            case R.id.zhifubao:
//                hideMode();
                payMode(ZHIFUBAO);
                break;
            case R.id.weixin:
//                hideMode();
                payMode(WEIXIN);
                break;
            case R.id.yinhangka:
//                hideMode();
                payMode(YINHANGKA);
                break;
            case R.id.buy_submit:
                break;
            case R.id.sold_out:
                break;
        }
    }

    private void viewSelected() {
        zhifubaoSelect.setSelected(false);
        weixinSelect.setSelected(false);
        yinhangkaSelect.setSelected(false);
        payMode(ZHIFUBAO);
    }

    private void payMode(int mode) {
        if (mode == ZHIFUBAO) {
            if (!zhifubaoSelect.isSelected()) {
                zhifubaoSelect.setSelected(true);
                zhifubaoSelect.setVisibility(View.VISIBLE);
            } else {
                zhifubaoSelect.setSelected(false);
                zhifubaoSelect.setVisibility(View.GONE);
            }
        }

        if (mode == WEIXIN) {
            if (!weixinSelect.isSelected()) {
                weixinSelect.setSelected(true);
                weixinSelect.setVisibility(View.VISIBLE);
            } else {
                weixinSelect.setSelected(false);
                weixinSelect.setVisibility(View.GONE);
            }
        }

        if (mode == YINHANGKA) {
            if (!yinhangkaSelect.isSelected()) {
                yinhangkaSelect.setSelected(true);
                yinhangkaSelect.setVisibility(View.VISIBLE);
            } else {
                yinhangkaSelect.setSelected(false);
                yinhangkaSelect.setVisibility(View.GONE);
            }
        }

    }


    private void hideMode() {
        yinhangkaSelect.setVisibility(View.GONE);
        weixinSelect.setVisibility(View.GONE);
        zhifubaoSelect.setVisibility(View.GONE);
    }

    private void release() {
        if (release_statu == 0) {
            release_statu = 1;
            release.setText(R.string.exit_release);
            release.setSelected(false);
            releaseSelect.setVisibility(View.GONE);
            releaseTrue.setVisibility(View.GONE);
            isRelease.setVisibility(View.VISIBLE);
        } else {
            release_statu = 0;
            release.setText(R.string.release);
            release.setSelected(true);
            releaseSelect.setVisibility(View.VISIBLE);
            releaseTrue.setVisibility(View.VISIBLE);
            isRelease.setVisibility(View.GONE);
        }
    }

    //弹出币种切换的PopupWindow及对其进行操作
    private void coinChange() {
        popup_view = View.inflate(getActivity(), R.layout.popup_change_coin, null);
        mListView = (ListView) popup_view.findViewById(R.id.mListView);
        mLinears = (LinearLayout) popup_view.findViewById(R.id.linear);
        coinChangeRequestAdapter();
        mWindow = new PopupWindow(popup_view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, false);
        mWindow.setFocusable(true);
        mWindow.setTouchable(true);
        mWindow.showAtLocation(mParentView, Gravity.TOP, 0, 0);
        //外部点击
        mLinears.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWindow.dismiss();
            }
        });
    }

    //设置币种切换列表适配器以及处理item点击事件
    private void coinChangeRequestAdapter() {
        changeAdapter = new CoinChangeAdapter(getActivity(), changeData, position);
        mListView.setAdapter(changeAdapter);

        changeAdapter.onClickView(new CoinChangeAdapter.ChangeSelect() {
            @Override
            public void select(MainBean.DataBean.ListBean coin_data) {
                mWindow.dismiss();
                changeViewData(coin_data);
            }
        });

    }

    //修改选中货币所对应的数据
    private void changeViewData(MainBean.DataBean.ListBean coin_data) {
        coinChange.setText(coin_data.getCoin_symbol());
        coinBalance.setText(coin_data.getBalance());
    }

    //请求已有币种数据
    private void coinChangeRequestData() {
        changeData = new ArrayList<>();
        changeData.clear();
        requestManager.httpPost(Api.BaseUrl + Api.Main_List, Api.getMainList(token, ""), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                MainBean mainBean = mGson.fromJson(Response, MainBean.class);
                if (mainBean.getCode() == 200) {
                    changeData.addAll(mainBean.getData().getList());
                    changeViewData(mainBean.getData().getList().get(position));
                    if (changeAdapter != null) {
                        changeAdapter.notifyDataSetChanged();
                    }
                } else {
                    Toast.makeText(getActivity(), mainBean.getMessage(), Toast.LENGTH_SHORT).show();
                    coinChangeRequestData();
                }
            }

            @Override
            public void onFailure(String error) {
                Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
                coinChangeRequestData();
            }
        });
    }

    //卖
    private void sell() {
        if (transAdapter != null) {
            transAdapter.statu = 1;
            transAdapter.notifyDataSetChanged();
        }
        sell.setSelected(true);
        buy.setSelected(false);
        buy.setTextColor(getResources().getColor(R.color.statu));
        sell.setTextColor(getResources().getColor(R.color.colorToolbarBack));
        selectMode.setText(getString(R.string.select_receivable_mode));
        buySubmit.setText(R.string.sell);
        sellStatu.setVisibility(View.VISIBLE);
        zifu.setVisibility(View.GONE);
    }

    //买
    private void buy() {
        if (transAdapter != null) {
            transAdapter.statu = 0;
            transAdapter.notifyDataSetChanged();
        }
        buy.setSelected(true);
        sell.setSelected(false);
        buy.setTextColor(getResources().getColor(R.color.colorToolbarBack));
        sell.setTextColor(getResources().getColor(R.color.statu));
        selectMode.setText(getString(R.string.select_pay_mode));
        buySubmit.setText(R.string.buy);
        sellStatu.setVisibility(View.GONE);
        zifu.setVisibility(View.VISIBLE);
    }
}
